/*
 * @Author: shuai
 * @Date: 2021-03-31 10:27:59
 * @LastEditors: shuai
 * @LastEditTime: 2021-04-04 09:47:49
 * @Description: file content
 */
import Cookies from 'js-cookie'
import { ActionContext } from 'vuex'
const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+(Cookies.get('sidebarStatus') ?? 1) : true,
    withoutAnimation: false,
  },
  device: 'desktop',
}

export type State = typeof state

const mutations = {
  TOGGLE_SIDEBAR: (state: State) => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', '1')
    } else {
      Cookies.set('sidebarStatus', '0')
    }
    // console.log(state.sidebar.opened)
  },
  CLOSE_SIDEBAR: (state: State, withoutAnimation: boolean) => {
    Cookies.set('sidebarStatus', '0')
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state: State, device: string) => {
    state.device = device
  },
}

const actions = {
  toggleSideBar({ commit }: ActionContext<State, unknown>) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }: ActionContext<State, unknown>, { withoutAnimation }: { withoutAnimation: boolean }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }: ActionContext<State, unknown>, device: string) {
    commit('TOGGLE_DEVICE', device)
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
